package com.fangtao.servlet;

import com.fangtao.domain.User;
import com.fangtao.service.UserService;
import com.fangtao.utils.Constants;
import com.sun.corba.se.impl.orbutil.closure.Constant;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * @ClassName LoginServlet
 * @Description TODO
 * @date 2022/9/22 17:02
 * @author: fangtao
 * @Version 1.0
 */
public class LoginServlet extends HttpServlet {
    private static UserService userService= new UserService();
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String userCode = req.getParameter("userCode");
        String password = req.getParameter("userPassword");
        User user = userService.Login(userCode, password);
        if (user!=null){
            req.getSession().setAttribute(Constants.USER_SESSION,user);

            resp.sendRedirect("/smb/jsp/frame.jsp");
        }else {
            req.setAttribute("error","账号或者密码错误");
            req.getRequestDispatcher("login.jsp").forward(req,resp);
        }

    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doGet(req, resp);
    }
}
